home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_200
/
227_01
/
grafturt.c
< prev
next >
Wrap
Text File
|
1988-02-07
|
16KB
|
592 lines
/*
* g r a f t u r t . c
* -------------------
* This module implements the turtle graphics package. Note that this
* package always operates with its own, global coordinate system. The
* resulution may by set by the function call turt_res() and is predefined
* to 801 * 401, which should be a good value for many video devices.
*
* Realease history
* --------------
* Jun, 20. 1987 Begin implemention.
*
* Written by Rainer Gerhards
* Petronellastr. 6
* D-5112 Baesweiler
* West Germany
* Phone (49) 2401 - 1601
*/
/*
* Define library implemention mode - forbidden for user!
*/
#define LIB_MODE 1
#include <stdio.h>
#include <dos.h>
#include <math.h>
#ifdef __TURBOC__
#include <conio.h>
#endif
#include "graphics.h"
/*
* -------------------------------------------------------------------------
* M A C R O S
* -------------------------------------------------------------------------
*/
#define last3bit(x) ((x) & 0x0007)
#define exclst3b(x) ((x) & ~0x0007)
#define DIR turt_dir
#define PEN_DN turt_pdn
#define PEN_UP !turt_pdn
#define X turt_x
#define Y turt_y
#define XC convxco((double)turt_x)
#define YC convyco((double)turt_y)
#define COLOR turt_clr
#define VISIBLE turt_vis
#define DELY_TIM turt_del
#define NXT_X(x, cnt, direct)\
((x) + (float) itocarxc((convxdis(\
convxrad(glo_maxx, glo_maxy,(cnt)))),\
(direct)))
#define NXT_Y(y, cnt, direct)\
((y) + (float) itocaryc((convydis((double) (cnt))), (direct)))
/*
* -------------------------------------------------------------------------
* S T A T I C D A T A I T E M S
* -------------------------------------------------------------------------
*/
static int turt_dir = GTDHOME; /* turtle direction in degrees */
/*
* -------------------------------------------------------------------------
* F U N C T I O N S
* -------------------------------------------------------------------------
*/
#ifdef USEPROTT
extern void clr_turt(void);
extern void set_turt(void);
#endif
static void clr_turt()
/*
* name clr_turt
*
* synopsis clr_turt();
*
* description This function clear the old turtle symbol (if the turtle
* is visible).
*
* Warning This function is internal to the turtle system only. It
* may be changed or removed without notice!
*/
{
double tx, ty; /* temporary turtle position */
int tdir; /* temporary direction */
if(VISIBLE == TRUE)
{
tx = NXT_X(X, (turt_sz/2.0), DIR);
ty = NXT_Y(Y, (turt_sz/2.0), DIR);
setwm(WM_INV);
if(PEN_DN)
line(XC, YC, convxco(tx), convyco(ty), WHITE);
if((tdir = DIR - 135) < 0)
tdir = 360 + tdir;
line(convxco(tx), convyco(ty),
(int) convxco(NXT_X(tx, -turt_sz, tdir)),
(int) convyco(NXT_Y(ty, -turt_sz, tdir)), WHITE
);
if((tdir = DIR + 135) > 360)
tdir -= 360;
line(convxco(tx), convyco(ty),
convxco(NXT_X(tx, -turt_sz, tdir)),
convyco(NXT_Y(ty, -turt_sz, tdir)), WHITE
);
setwm(WM_NORM);
}
}
static void set_turt()
/*
* name set_turt
*
* synopsis set_turt();
*
* description This function redraws the turtle symbol after the turtle
* position has changed. If the turtle is invisible, no symbol
* is drawn.
* This function is also responsible for the turtle delay.
*
* Warning This function is internal to the turtle system only. It
* may be changed or removed without notice!
*/
{
clr_turt();
}
void gt_show()
/*
* name gt_show
*
* synopsis gt_show()
*
* description The turtle is shown after each drawing operation if
* this function is called.
*/
{
clr_turt();
VISIBLE = TRUE;
set_turt();
}
void gt_hide()
/*
* name gt_show
*
* synopsis gt_show()
*
* description The turtle isn't shown anytime after this function is called.
*/
{
clr_turt();
VISIBLE = FALSE;
set_turt();
}
void gt_forwd(count)
int count;
/*
* name gt_forwd
*
* synopsis gt_forwd(count)
* int count; count to move foreward
*
* description This function moves the turtle forward the specfied count
* of 'logical' pixel (or backward, if negative).
*/
{
double tx, ty;
clr_turt(); /* erase old turtle */
tx = NXT_X(X, count, DIR);
ty = NXT_Y(Y, count, DIR);
if(PEN_DN)
line(XC, YC, convxco((double) tx), convyco((double) ty), COLOR);
X = tx;
Y = ty;
set_turt(); /* mark new turtle position */
}
#if USEVOID
void
#endif
gt_backw(count)
int count;
/*
* name gt_backw
*
* synopsis gt_backw(count)
* int count; count to move backward
*
* description This function is the opposite to function gt_forwd.
* It moves the turtle the specified position in backward
* direction.
* Internaly this results in a call to function gt_forwd,
* and because this is so simple the function is available
* as macro too. So you should use arguments without side-
* effekt to be as portable as possible.
*/
{
gt_forwd(-count);
}
#if USEVOID
void
#endif
gt_dir(degrees)
int degrees;
/*
* name gt_dir
*
* synopsis gt_dir(degrees);
* int degrees; new turtle direction in degrees.
*
* description This function sets the absolute turtle direction in
* degrees for further moves. If the value contained in
* "degrees" is invalid, it will be converted into the
* range 0..359. Negative degrees will be correctly converted
* to their positve counterpart.
*/
{
int wasneg = 0;
clr_turt(); /* erase old turtle */
degrees += 90;
if(degrees < 0)
{
degrees = -degrees;
wasneg = -1;
}
while(degrees > 359)
degrees -= 360;
DIR = wasneg ? 360 - degrees : degrees;
set_turt(); /* mark new turtle position */
}
#if USEVOID
void
#endif
gt_tleft(degrees)
int degrees;
/*
* name gt_tleft
*
* synopsis gt_tleft(degrees);
* int degrees; turtle rotate count in degrees
*
* description This function rotates the turtle in left direction, using
* the rotate count specified in degrees. Negative counts mean
* rotate to rigth position.
*/
{
gt_dir(DIR + degrees - 90);
}
#if USEVOID
void
#endif
gt_trght(degrees)
int degrees;
/*
* name gt_trght
*
* synopsis gt_tleft(degrees);
* int degrees; turtle rotate count in degrees
*
* description This function rotates the turtle in rigth direction, using
* the rotate count specified in degrees. Negative counts mean
* rotate to left position.
*/
{
gt_tleft(-degrees);
}
#if USEVOID
void
#endif
gt_setco(x, y)
int x, y;
/*
* name gt_setco
*
* synopsis gt_setco(x, y);
* int x,y; new turtle coordinates.
*
* description This function sets the turtle to the given position. The
* position is a turtle graphics coordinate. No line is draw
* while repositioning the turtle.
*/
{
clr_turt(); /* erase old turtle */
X = x;
Y = y;
set_turt(); /* mark new turtle position */
}
int gtgetdir()
/*
* name gtgetdir
*
* synopsis degrees = gtgetdir();
* int degrees; current turtle direction in degrees.
*
* description This function returns the current turtle position in degrees.
* The returned value will be in range 0 .. 359.
*/
{
int direct;
direct = DIR - 90;
return((direct < 0) ? 360 + direct : direct);
}
#if USEVOID
void
#endif
gt_home()
/*
* name gt_home
*
* synopsis